﻿// Decompiled by AS3 Sorcerer 6.12
// www.as3sorcerer.com

//LevelEditor

package 
{
    import flash.display.Sprite;
    import flash.filters.GlowFilter;
    import ui.SimpleGUI;
    import com.bit101.components.ComboBox;
    import com.bit101.components.HUISlider;
    import flash.display.Graphics;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import com.bit101.components.Panel;
    import com.bit101.components.HBox;

    public class LevelEditor extends Sprite 
    {

        private const _SafeStr_152:int = 20;
        private const _SafeStr_167:GlowFilter = new GlowFilter(0xFFFF00, 0.8, 6, 6);
        private const _SafeStr_159:int = 0;
        private const _SafeStr_160:int = 220;
        private const _SafeStr_156:int = 1;
        private const _SafeStr_161:int = 150;
        private const _SafeStr_155:GlowFilter = new GlowFilter(0xFFFF00, 0.8, 10, 10);

        private var _SafeStr_148:SimpleGUI = null;
        private var _SafeStr_149:ComboBox = null;
        private var _SafeStr_151:int = 3;
        private var _aiSpeed:ComboBox = null;
        private var _SafeStr_166:int = 10;
        private var _SafeStr_165:Boolean = false;
        private var _SafeStr_154:Fence = null;
        private var _maxPower:HUISlider = null;
        private var _lvl:Level = null;
        private var _SafeStr_164:Boolean = false;
        private var _SafeStr_163:Boolean = false;
        private var _SafeStr_150:Cell = null;

        public function LevelEditor(game:Main, levelCode:String)
        {
            this._lvl = new Level(game, "", 200);
            this._lvl.suspend();
            if (levelCode)
            {
                this._lvl.initFromCode(levelCode);
            };
            addChild(this._lvl);
            this._SafeStr_168();
            var g:Graphics = graphics;
            g.beginFill(0, 0);
            g.drawRect(0, 0, GUI.MAX_X, GUI.MAX_Y);
            g.endFill();
            addEventListener(MouseEvent.MOUSE_DOWN, this._SafeStr_145);
            addEventListener(MouseEvent.MOUSE_UP, this._SafeStr_146);
            addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
            if (stage)
            {
                this._SafeStr_147();
            }
            else
            {
                addEventListener(Event.ADDED_TO_STAGE, this._SafeStr_147);
            };
        }

        public function get maxPower():int
        {
            return (this._lvl.maxPower);
        }

        public function set maxPower(power:int):void
        {
            this._lvl.maxPower = power;
        }

        public function get selectedCellType():int
        {
            return ((this._SafeStr_150) ? this._SafeStr_150.type : this._SafeStr_151);
        }

        private function _SafeStr_153(fence:Fence, pivotX:Number, pivotY:Number):void
        {
            var dx:Number;
            var dy:Number;
            var dist1:Number;
            var dist2:Number;
            if (this._SafeStr_154)
            {
                this._SafeStr_154.filters = [];
                if (this._SafeStr_148)
                {
                    this._SafeStr_148.hide();
                    this._SafeStr_148 = null;
                };
            };
            this._SafeStr_154 = fence;
            if (fence)
            {
                this._SafeStr_164 = true;
                this._SafeStr_154.filters = [this._SafeStr_155];
                dx = (fence.x1 - pivotX);
                dy = (fence.y1 - pivotY);
                dist1 = Math.sqrt(((dx * dx) + (dy * dy)));
                dx = (fence.x2 - pivotX);
                dy = (fence.y2 - pivotY);
                dist2 = Math.sqrt(((dx * dx) + (dy * dy)));
                this._SafeStr_165 = ((dist2 > dist1) ? true : false);
                if (!this._SafeStr_148)
                {
                    this._SafeStr_148 = this._SafeStr_158(this._SafeStr_156);
                };
            };
        }

        public function destruct():void
        {
            removeEventListener(MouseEvent.MOUSE_DOWN, this._SafeStr_145);
            removeEventListener(MouseEvent.MOUSE_UP, this._SafeStr_146);
            removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, this._SafeStr_157);
            this._lvl.destruct();
            this._lvl = null;
            if (parent)
            {
                parent.removeChild(this);
            };
        }

        private function _SafeStr_158(_arg_1:int):SimpleGUI
        {
            var gui:SimpleGUI = new SimpleGUI(this, ((_arg_1 == this._SafeStr_159) ? GUI.TXT_MICROBE : GUI.TXT_WALL));
            gui.setPos(0, this._SafeStr_152);
            if (this._SafeStr_150)
            {
                if (((this._SafeStr_150.x < this._SafeStr_160) && (this._SafeStr_150.y < this._SafeStr_161)))
                {
                    gui.setPos((this._SafeStr_160 + 20), this._SafeStr_152);
                };
            };
            if (_arg_1 == this._SafeStr_159)
            {
                gui.addComboBox("selectedCellType", [{
                    "label":GUI.TXT_GREEN,
                    "data":Cell.TYPE_GREEN
                }, {
                    "label":GUI.TXT_RED,
                    "data":Cell.TYPE_RED
                }, {
                    "label":GUI.TXT_BLACK,
                    "data":Cell.TYPE_BLACK
                }, {
                    "label":GUI.TXT_PURPLE,
                    "data":Cell.TYPE_PURPLE
                }, {
                    "label":GUI.TXT_NEUTRAL,
                    "data":Cell.TYPE_GREY
                }], {
                    "label":GUI.TXT_CLASS,
                    "width":"120"
                });
                gui.addSlider("selectedCellPower", 5, 200, {
                    "label":GUI.TXT_POWER,
                    "labelPrecision":-1,
                    "width":180
                });
                gui.addButton(GUI.TXT_DELETE_MICROBE, {
                    "callback":this._SafeStr_162,
                    "width":150
                });
            }
            else
            {
                gui.addButton(GUI.TXT_DELETE_WALL, {
                    "callback":this._SafeStr_162,
                    "width":150
                });
            };
            gui.show();
            return (gui);
        }

        private function _SafeStr_147(e:Event=null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, this._SafeStr_147);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, this._SafeStr_157);
        }

        private function onMouseMove(e:MouseEvent):void
        {
            if (this._SafeStr_163)
            {
                return;
            };
            if (((this._SafeStr_164) && (e.buttonDown)))
            {
                if (this._SafeStr_150)
                {
                    this._SafeStr_150.x = e.stageX;
                    this._SafeStr_150.y = e.stageY;
                }
                else
                {
                    if (this._SafeStr_154)
                    {
                        if (this._SafeStr_165)
                        {
                            this._SafeStr_154.x1 = e.stageX;
                            this._SafeStr_154.y1 = e.stageY;
                        }
                        else
                        {
                            this._SafeStr_154.x2 = e.stageX;
                            this._SafeStr_154.y2 = e.stageY;
                        };
                    };
                };
                if (this._SafeStr_148)
                {
                    if (((e.stageX < this._SafeStr_160) && (e.stageY < this._SafeStr_161)))
                    {
                        this._SafeStr_148.setPos((this._SafeStr_160 + 20), this._SafeStr_152);
                    }
                    else
                    {
                        this._SafeStr_148.setPos(0, this._SafeStr_152);
                    };
                };
            };
        }

        private function _SafeStr_146(e:MouseEvent):void
        {
            this._SafeStr_164 = false;
        }

        private function _SafeStr_157(e:KeyboardEvent):void
        {
            if (e.keyCode == 46)
            {
                this._SafeStr_162();
            };
        }

        private function _SafeStr_145(e:MouseEvent):void
        {
            var cell:Cell;
            var fence:Fence;
            if (this._SafeStr_163)
            {
                return;
            };
            if ((e.target is Cell))
            {
                this._SafeStr_153(null, 0, 0);
                this._SafeStr_169((e.target as Cell));
            }
            else
            {
                if ((e.target is Fence))
                {
                    this._SafeStr_169(null);
                    this._SafeStr_153((e.target as Fence), e.stageX, e.stageY);
                };
            };
            if (e.target == this)
            {
                this._SafeStr_169(null);
                this._SafeStr_153(null, 0, 0);
                if (this._SafeStr_149.selectedIndex == this._SafeStr_159)
                {
                    cell = this._lvl.addCell(e.stageX, e.stageY, this._SafeStr_151, this._SafeStr_166, this._lvl.maxPower);
                    if (cell != null)
                    {
                        cell.suspend();
                    };
                    this._SafeStr_169(cell);
                }
                else
                {
                    if (this._SafeStr_149.selectedIndex == this._SafeStr_156)
                    {
                        fence = this._lvl.addFence(e.stageX, e.stageY, e.stageX, e.stageY);
                        this._SafeStr_153(fence, e.stageX, e.stageY);
                    };
                };
            };
        }

        public function set selectedCellPower(power:int):void
        {
            if (this._SafeStr_150)
            {
                this._SafeStr_150.power = power;
            };
            this._SafeStr_166 = power;
        }

        private function _SafeStr_168():void
        {
            var win:Panel = new Panel(this);
            win.shadow = false;
            win.width = 640;
            win.height = 19;
            var box:HBox = new HBox(this);
            box.spacing = 10;
            this._SafeStr_149 = new ComboBox(box);
            this._SafeStr_149.addItem(GUI.TXT_CREATE_MICROBE);
            this._SafeStr_149.addItem(GUI.TXT_CREATE_WALL);
            this._SafeStr_149.selectedIndex = 0;
            this._SafeStr_149.numVisibleItems = 2;
            this._SafeStr_149.width = 150;
            this._aiSpeed = new ComboBox(box);
            this._aiSpeed.addItem(GUI.TXT_NORMAL_AI);
            this._aiSpeed.addItem(GUI.TXT_QUICK_AI);
            this._aiSpeed.addItem(GUI.TXT_INSTANT_AI);
            this._aiSpeed.addItem(GUI.TXT_DISABLE_AI);
            this._aiSpeed.selectedIndex = 0;
            this._aiSpeed.numVisibleItems = 4;
            this._aiSpeed.width = 150;
            if (this._lvl.cpuTurnPeriod >= 90)
            {
                this._aiSpeed.selectedIndex = 0;
            }
            else
            {
                if (this._lvl.cpuTurnPeriod >= 40)
                {
                    this._aiSpeed.selectedIndex = 1;
                }
                else
                {
                    if (this._lvl.cpuTurnPeriod > 0)
                    {
                        this._aiSpeed.selectedIndex = 2;
                    }
                    else
                    {
                        if (this._lvl.cpuTurnPeriod == 0)
                        {
                            this._aiSpeed.selectedIndex = 3;
                        };
                    };
                };
            };
            this._aiSpeed.addEventListener(Event.SELECT, function (e:Event):void
            {
                trace(("AI speed: " + _aiSpeed.selectedIndex));
                switch (_aiSpeed.selectedIndex)
                {
                    case 0:
                        _lvl.cpuTurnPeriod = 100;
                        return;
                    case 1:
                        _lvl.cpuTurnPeriod = 50;
                        return;
                    case 2:
                        _lvl.cpuTurnPeriod = 10;
                        return;
                    case 3:
                        _lvl.cpuTurnPeriod = 0;
                        return;
                };
            });
            this._maxPower = new HUISlider(box, 0, 0, GUI.TXT_MAX_POWER, function (e:Event):void
            {
                maxPower = _maxPower.value;
            });
            this._maxPower.minimum = 10;
            this._maxPower.maximum = 200;
            this._maxPower.value = this.maxPower;
            this._maxPower.width = 250;
            this._maxPower.labelPrecision = -1;
        }

        public function getLevelCode():String
        {
            return (this._lvl.getCode());
        }

        public function get selectedCellPower():int
        {
            return ((this._SafeStr_150) ? this._SafeStr_150.power : this._SafeStr_166);
        }

        public function set selectedCellType(_arg_1:int):void
        {
            if (this._SafeStr_150)
            {
                this._SafeStr_150.type = _arg_1;
            };
            this._SafeStr_151 = _arg_1;
        }

        private function _SafeStr_162():void
        {
            if (this._SafeStr_150)
            {
                this._lvl.delCell(this._SafeStr_150);
                this._SafeStr_169(null);
            }
            else
            {
                if (this._SafeStr_154)
                {
                    this._lvl.delFence(this._SafeStr_154);
                    this._SafeStr_153(null, 0, 0);
                };
            };
        }

        private function _SafeStr_169(cell:Cell):void
        {
            if (this._SafeStr_150)
            {
                this._SafeStr_150.filters = [];
                this._SafeStr_150 = null;
                if (this._SafeStr_148)
                {
                    this._SafeStr_148.hide();
                    this._SafeStr_148 = null;
                };
            };
            if (cell)
            {
                this._SafeStr_164 = true;
                this._SafeStr_150 = cell;
                this._SafeStr_150.filters = [this._SafeStr_167];
                if (!this._SafeStr_148)
                {
                    this._SafeStr_148 = this._SafeStr_158(this._SafeStr_159);
                };
                this._SafeStr_151 = cell.type;
                this._SafeStr_166 = cell.power;
            };
        }


    }
}//package 

// _SafeStr_145 = "_-3S" (String#4818, DoABC#2)
// _SafeStr_146 = "_-8q" (String#5073, DoABC#2)
// _SafeStr_147 = "_-61" (String#4937, DoABC#2)
// _SafeStr_148 = "_-51" (String#4889, DoABC#2)
// _SafeStr_149 = "_-1U" (String#4724, DoABC#2)
// _SafeStr_150 = "_-3a" (String#4824, DoABC#2)
// _SafeStr_151 = "_-CD" (String#5236, DoABC#2)
// _SafeStr_152 = "_-Ag" (String#5165, DoABC#2)
// _SafeStr_153 = "_-5E" (String#4899, DoABC#2)
// _SafeStr_154 = "package" (String#7066, DoABC#2)
// _SafeStr_155 = "function" (String#6290, DoABC#2)
// _SafeStr_156 = "_-4D" (String#4850, DoABC#2)
// _SafeStr_157 = "default" (String#6040, DoABC#2)
// _SafeStr_158 = "_-7x" (String#5030, DoABC#2)
// _SafeStr_159 = "_-5l" (String#4924, DoABC#2)
// _SafeStr_160 = "_-9m" (String#5117, DoABC#2)
// _SafeStr_161 = "true " (String#7628, DoABC#2)
// _SafeStr_162 = "_-25" (String#4754, DoABC#2)
// _SafeStr_163 = "_-60" (String#4936, DoABC#2)
// _SafeStr_164 = "_-33" (String#4799, DoABC#2)
// _SafeStr_165 = "_-0X" (String#4678, DoABC#2)
// _SafeStr_166 = "_-0i" (String#4686, DoABC#2)
// _SafeStr_167 = "_-Az" (String#5181, DoABC#2)
// _SafeStr_168 = "_-0F" (String#4668, DoABC#2)
// _SafeStr_169 = "_-3I" (String#4811, DoABC#2)


